<template>
    <div class="bind-phone">
        <div class="tag">
            登录或注册页面
        </div>
        <br/>
        <br/>
        <div class="form">
            <x-input title="手机号" label-width="60px" placeholder="请输入手机号" v-model="phone"></x-input>
            <x-input title="密码" label-width="60px" placeholder="请输入密码" type="password" v-model="password"></x-input>
            <br/>
            <br/>
            <x-button type="primary" @click.native="save">登录/注册</x-button>
        </div>
    </div>
</template>
<script>
import { XInput, XButton } from "vux";
export default {
  data() {
    return {
      phone: null,
      password: null
    };
  },
  components: {
    XInput,
    XButton
  },
  methods: {
    save() {
      // this.$vux.toast.text("请输入手机号", "center")

      // this.$emit("back");
      // return;

      let _this = this;

      if (!_this.phone) {
        _this.$vux.toast.text("请输入手机号", "center");
        return;
      }
      if (!_this.password) {
        _this.$vux.toast.text("请输入密码", "center");
        return;
      }

      _this.$$api_user_loginUser({
        data: { phone: _this.phone, password: _this.password },
        fn: res => {
          // console.log(" res.userId", res);
          this.$store
            .dispatch("update_user", res.userId)
            .then(result => this.$emit("back"));
        },
        errFn: res => {
          console.log(" err res", res);

          if (res.status == 200011) {
            this.$vux.toast.text(res.msg, "center");
            return;
          }
          if (res.status == 200012) {
            // this.$vux.toast.text(res.msg, "center");

            this.$$api_user_addUser({
              data: {
                phone: this.phone,
                password: this.password
              },
              fn: res => {
                // this.goBack();
                console.log(" res.userId", res.userId);
                this.$store
                  .dispatch("update_user", res.userId)
                  .then(result => this.$emit("back"));
              },
              errFn: res => {
                if (res.status == 200010) {
                  this.$vux.toast.text(res.msg, "center");
                } else {
                  this.$vux.toast.text("服务器开小差了！", "center");
                }
              }
            });

            return;
          }
        }
      });
    },
    queryStudentCountByUserId(_userId) {
      let _this = this;

      th_thisis.$$api_user_queryStudentCountByUserId({
        data: _userId,
        fn: function(res) {
          if (res.count > 0) {
            _this.$emit("back");
          }
        },
        errFn: function(res) {
          _this.$vux.toast.text("服务器开小差了！", "center");
        }
      });
    }
  }
};
</script>


<style lang="scss" scoped>
@import "~@/assets/scss/base.scss";
@import "~@/assets/scss/const.scss";
@import "~@/assets/scss/mixin.scss";

.bind-phone {
  z-index: 1000;
  position: fixed;
  z-index: 1000;
  top: 0;
  left: 0;
  height: 100vh;
  width: 100vw;
  overflow: auto;
  background: $app-background-color;
  .tag {
    width: 100%;
    height: 40px;
    line-height: 40px;
    color: rgba(0, 0, 0, 1);
    text-align: center;
    font-size: 14px;
    font-weight: bold;
    border: none;
    background: #f2f2f2;
  }
  .form {
    padding: 15px;
    padding-top: 30px;
    .mint-button {
      margin-top: 50px;
    }
  }
}
</style>
